import Group from './group'
import GroupToPermission from './groupToPermission'
import GroupToRole from './groupToRole'
import Permission from './permission'
import Role from './role'
import RoleToPermission from './roleToPermission'
import UserGroup from './userGroup'
import UserPermission from './userPermission'
import UserRole from './userRole'
import User from './user'
import { isArray, isObject } from '../utils/index'
/**
 * 权限验证
 * @description
 * 权限分为三种：
 * 一：路由方面，用户登录后只能看到自己有权访问的导航，也只能访问自己有权访问的路由地址，否则将跳转 4xx 提示页；
 * // 设置页面跳转方法的拦截器，页面跳转方法哪里直接配置
 * 二：视图方面，用户只能看到自己有权浏览的内容和有权操作的控件【按钮等】；
 * // 自定义指令用于试图元素的权限验证
 *    Vue.directive('has', {
 *      bind: (el, binding) => {
 *        if (!Vue.prototype.$_has(binding.value)) {
 *          el.parentNode.removeChild(el)
 *        }
 *      }
 *    })
 * 三：最后再加上请求控制作为最后一道防线，路由可能配置失误，按钮可能忘了加权限，这种时候请求控制可以用来兜底，越权请求将在前端被拦截。
 * // 这个在请求的拦截器哪里直接配置
 */
export class Auth {
  constructor() {
    this.role = Role // 角色
    this.group = Group // 分组
    this.permission = Permission // 权限
    this.user = User // 用户

    this.userGroup = UserGroup // 用户对应分组
    this.userRole = UserRole // 用户对应角色
    this.groupToRole = GroupToRole // 分组对应角色

    this.roleToPermission = RoleToPermission // 角色对应权限
    this.groupToPermission = GroupToPermission // 分组对应权限
    this.userPermission = UserPermission // 用户对应权限
  }

  getPermission(uid) {
    let user = this.user.getState(uid)
    if (!user) {
      return []
    }
    let userGroup = this.userGroup.getState({ uid: user.uid })
    let userRole = this.userRole.getState({ uid: user.uid })
    // TODO 此处未处理分组和角色交叉的情况

    let userPermission = []
    // 用户权限
    userPermission = userPermission.concat(
      this.userPermission.getState({ uid: user.uid })
    )
    // 分组权限
    let groupToPermission = []
    for (let i = 0, leng = userGroup.length; i < leng; i++) {
      groupToPermission = groupToPermission.concat(
        this.groupToPermission.getState({
          gid: userGroup[i].gid
        })
      )
    }
    // 角色权限
    let roleToPermission = []
    for (let i = 0, leng = userRole.length; i < leng; i++) {
      roleToPermission = roleToPermission.concat(
        this.roleToPermission.getState({
          rid: userRole[i].rid
        })
      )
    }

    // 权限
    let permission = []
    for (let i = 0, leng = userPermission.length; i < leng; i++) {
      permission.push(this.permission.getState(userPermission[i].pid))
    }
    for (let i = 0, leng = groupToPermission.length; i < leng; i++) {
      permission.push(this.permission.getState(groupToPermission[i].pid))
    }
    for (let i = 0, leng = roleToPermission.length; i < leng; i++) {
      permission.push(this.permission.getState(roleToPermission[i].pid))
    }

    let key = {}
    let result = []
    for (let i = 0; i < permission.length; i++) {
      const item = permission[i]
      if (!key[item.pid]) {
        key[item.pid] = true
        result.push(item)
      } else {
        continue
      }
    }
    // 合并权限
    // console.log(result)
  }

  /**
   * 检测是否存在某个权限
   * @param {*} pid
   * @param {*} uid
   */
  checkPermission(pid, uid) {
    let permission = this.getPermission(uid)
    if (permission.length > 0) {
      let valid = permission.some(v => {
        return v.pid === pid
      })
      return valid
    }
    return false
  }

  /**
   * 设置用户
   * @param {array|object} rids [{name: '', uid: ''}] or {name: '', uid: ''}
   */
  setUser(users) {
    // 判断rid的类型
    if (isObject(users)) {
      return this.user.setState(users)
    }
    if (isArray(users)) {
      for (let i = 0, leng = users.length; i < leng; i++) {
        if (isObject(users[i])) {
          this.setUser(users[i])
        }
      }
    }
  }
  /**
   * 设置角色
   * @param {array|object} rids [{name: '', rid: ''}] or {name: '', rid: ''}
   */
  setRole(rids) {
    // 判断rid的类型
    if (isObject(rids)) {
      return this.role.setState(rids)
    }
    if (isArray(rids)) {
      for (let i = 0, leng = rids.length; i < leng; i++) {
        if (isObject(rids[i])) {
          this.setRole(rids[i])
        }
      }
    }
  }
  /**
   * 设置分组
   * @param {array|object} gids [{name: '', gid: ''}] or {name: '', gid: ''}
   */
  setGroup(gids) {
    // 判断gid的类型
    if (isObject(gids)) {
      return this.group.setState(gids)
    }
    if (isArray(gids)) {
      for (let i = 0, leng = gids.length; i < leng; i++) {
        if (isObject(gids[i])) {
          this.setGroup(gids[i])
        }
      }
    }
  }
  /**
   * 设置权限
   * @param {array|object} pids [{name: '', pid: ''}] or {name: '', pid: ''}
   */
  setPermission(pids) {
    // 判断rid的类型
    if (isObject(pids)) {
      return this.permission.setState(pids)
    }
    if (isArray(pids)) {
      for (let i = 0, leng = pids.length; i < leng; i++) {
        if (isObject(pids[i])) {
          this.setPermission(pids[i])
        }
      }
    }
  }
  /**
   * 设置用户对应分组
   * @param {array|object} userGroups [{uid: '', gid: ''}] or {uid: '', gid: ''}
   */
  setUserGroup(userGroups) {
    // 判断rid的类型
    if (isObject(userGroups)) {
      return this.userGroup.setState(userGroups)
    }
    if (isArray(userGroups)) {
      for (let i = 0, leng = userGroups.length; i < leng; i++) {
        if (isObject(userGroups[i])) {
          this.setUserGroup(userGroups[i])
        }
      }
    }
  }
  /**
   * 用户对应角色
   * @param {array|object} userRoles [{uid: '', rid: ''}] or {uid: '', rid: ''}
   */
  setUserRoles(userRoles) {
    // 判断rid的类型
    if (isObject(userRoles)) {
      return this.userRole.setState(userRoles)
    }
    if (isArray(userRoles)) {
      for (let i = 0, leng = userRoles.length; i < leng; i++) {
        if (isObject(userRoles[i])) {
          this.setUserRoles(userRoles[i])
        }
      }
    }
  }
  /**
   * 分组对应角色
   * @param {array|object} groupToRoles [{rid: '', gid: ''}] or {rid: '', gid: ''}
   */
  setGroupToRole(groupToRoles) {
    // 判断rid的类型
    if (isObject(groupToRoles)) {
      return this.groupToRole.setState(groupToRoles)
    }
    if (isArray(groupToRoles)) {
      for (let i = 0, leng = groupToRoles.length; i < leng; i++) {
        if (isObject(groupToRoles[i])) {
          this.setGroupToRole(groupToRoles[i])
        }
      }
    }
  }
  /**
   * 角色对应权限
   * @param {array|object} roleToPermissions [{rid: '', pid: ''}] or {rid: '', pid: ''}
   */
  setRoleToPermission(roleToPermissions) {
    // 判断rid的类型
    if (isObject(roleToPermissions)) {
      return this.roleToPermission.setState(roleToPermissions)
    }
    if (isArray(roleToPermissions)) {
      for (let i = 0, leng = roleToPermissions.length; i < leng; i++) {
        if (isObject(roleToPermissions[i])) {
          this.setRoleToPermission(roleToPermissions[i])
        }
      }
    }
  }
  /**
   * 分组对应权限
   * @param {array|object} groupToPermissions [{pid: '', gid: ''}] or {pid: '', gid: ''}
   */
  setGroupToPermission(groupToPermissions) {
    // 判断rid的类型
    if (isObject(groupToPermissions)) {
      return this.groupToPermission.setState(groupToPermissions)
    }
    if (isArray(groupToPermissions)) {
      for (let i = 0, leng = groupToPermissions.length; i < leng; i++) {
        if (isObject(groupToPermissions[i])) {
          this.setGroupToPermission(groupToPermissions[i])
        }
      }
    }
  }
  /**
   * 用户对应权限
   * @param {array|object} groupToRoles [{uid: '', pid: ''}] or {uid: '', pid: ''}
   */
  setUserPermission(userPermissions) {
    // 判断rid的类型
    if (isObject(userPermissions)) {
      return this.userPermission.setState(userPermissions)
    }
    if (isArray(userPermissions)) {
      for (let i = 0, leng = userPermissions.length; i < leng; i++) {
        if (isObject(userPermissions[i])) {
          this.setUserPermission(userPermissions[i])
        }
      }
    }
  }
}
